import { createSlice } from "@reduxjs/toolkit";
import { getCurrentCity } from "../../utils";

//城市相關
const cityStore = createSlice({
    name: 'user',
    initialState: {
        city: JSON.parse(localStorage.getItem('hkzf_city')) || ''
    },
    reducers: {
        _setCity(state,action){
            state.city = action.payload
        }
    }
})

const {_setCity} = cityStore.actions

//異步請求
const fetchCurCity = () => {
    return async (dispatch) => {
        const res = await getCurrentCity()
        dispatch(_setCity(res))
    }
}

export {fetchCurCity}
const cityReducer = cityStore.reducer
export default cityReducer